package com.wxb.shmily.redis;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * Created by Administrator on 2017/3/22.
 */
public enum redis_common {
    REDIS;

    private Logger log = LoggerFactory.getLogger(redis_common.class);
    private Jedis jedis=null;
    private JedisPool jedisPool = null;

    private redis_common(){
        //连接redis服务器
        jedis = new Jedis("60.205.217.220",6379);
        //权限认证
        jedis.auth("wxb123");
    }

    //获取一个jedis实例
    public Jedis getJedis(){
        //获取jedis连接池对象
        jedisPool = RedisCacheConfiguration.REDISFACTORY.getInstance();
        jedis = jedisPool.getResource();
        return jedis;
    }

    public void closeJedis(Jedis jedis){
        if (null != jedis){
            jedis.close();
            if (jedis.isConnected()) {
                try {
                    log.info("退出"+jedis.toString()+":"+jedis.quit());
                    jedis.disconnect();
                } catch (Exception e) {
                    log.error("退出失败，失败原因：", e.getMessage() , e);
                }
            }
            jedis.close();
        }
    }

    public static void main(String[] args) {
        Jedis jedis = new Jedis("183.128.221.138",6379);
        jedis.auth("wxb1993");
        //jedis.set("aa","11111");
        System.out.println(jedis.get("1"));
    }
}
